<template>
  <div class="app-container">
    <!-- 状态筛选 -->
    <div class="control-bar">
      <el-radio-group v-model="activeStatus" @change="filterOrders">
        <el-radio-button label="all">全部订单</el-radio-button>
        <el-radio-button label="pending">待处理（{{ pendingCount }}）</el-radio-button>
        <el-radio-button label="cooking">制作中（{{ cookingCount }}）</el-radio-button>
      </el-radio-group>

      <div class="right">
        <el-button type="warning" @click="refreshOrders">
          <el-icon><Refresh /></el-icon>刷新数据
        </el-button>
      </div>
    </div>

    <!-- 订单时间线 -->
    <el-scrollbar height="calc(100vh - 160px)">
      <div class="order-list">
        <div
          v-for="order in filteredOrders"
          :key="order.id"
          class="order-card"
          :class="{'highlight-order': order.priority === 1}"
        >
          <div class="order-header">
            <div class="order-meta">
              <span class="table-no">桌号：{{ order.table }}</span>
              <span class="order-time">下单时间：{{ formatTime(order.createTime) }}</span>
              <el-tag
                :type="statusStyle[order.status].type"
                effect="light"
                class="status-tag"
              >
                {{ statusStyle[order.status].text }}
              </el-tag>
              <el-tag v-if="order.priority === 1" type="danger" effect="dark">优先订单</el-tag>
            </div>
            <div class="total-info">
              共 {{ totalDishes(order) }} 道菜 | 总数量：{{ totalQuantity(order) }}
            </div>
          </div>

          <!-- 菜品详情 -->
          <div class="dish-list">
            <div
              v-for="dish in order.dishes"
              :key="dish.id"
              class="dish-item"
              :class="dish.status"
            >
              <div class="dish-info">
                <el-image
                  class="dish-image"
                  :src="dish.image"
                  fit="cover"
                  :preview-src-list="[dish.image]"
                />
                <div class="dish-detail">
                  <h4>{{ dish.name }}</h4>
                  <div class="meta">
                    <span class="quantity">需要数量：{{ dish.quantity }}</span>
                    <span class="cook-time">预计用时：{{ dish.cookTime }}分钟</span>
                  </div>
                </div>
              </div>

              <div class="dish-actions">
                <el-button
                  v-if="dish.status === 'pending'"
                  type="primary"
                  size="small"
                  @click="startCooking(dish)"
                >
                  开始制作
                </el-button>

                <el-button
                  v-if="dish.status === 'cooking'"
                  type="success"
                  size="small"
                  @click="completeDish(dish)"
                >
                  完成菜品
                </el-button>

                <el-tag
                  :type="dishStatusStyle[dish.status].type"
                  effect="plain"
                  class="status-tag"
                >
                  {{ dishStatusStyle[dish.status].text }}
                </el-tag>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-scrollbar>

    <!-- 语音提醒 -->
    <audio ref="alertSound" src="/static/alert.mp3"></audio>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 状态配置
const statusStyle = {
  0: { text: '待处理', type: 'warning' },
  1: { text: '制作中', type: 'primary' },
  2: { text: '已完成', type: 'success' }
}

const dishStatusStyle = {
  pending: { text: '等待中', type: 'info' },
  cooking: { text: '制作中', type: 'warning' },
  completed: { text: '已完成', type: 'success' }
}

// 模拟数据
const orders = ref([
  {
    id: 1,
    table: 'A01',
    status: 0,
    priority: 1,
    createTime: '2023-11-22 18:30:45',
    dishes: [
      {
        id: 101,
        name: '菲力牛排',
        quantity: 2,
        cookTime: 15,
        image: 'https://source.unsplash.com/random/200x200?steak',
        status: 'pending'
      },
      {
        id: 102,
        name: '龙虾浓汤',
        quantity: 2,
        cookTime: 10,
        image: 'https://source.unsplash.com/random/200x200?soup',
        status: 'pending'
      }
    ]
  },
  {
    id: 2,
    table: 'B02',
    status: 1,
    priority: 0,
    createTime: '2023-11-22 18:25:30',
    dishes: [
      {
        id: 201,
        name: '海鲜披萨',
        quantity: 1,
        cookTime: 20,
        image: 'https://source.unsplash.com/random/200x200?pizza',
        status: 'cooking'
      },
      {
        id: 202,
        name: '蔬菜沙拉',
        quantity: 3,
        cookTime: 5,
        image: 'https://source.unsplash.com/random/200x200?salad',
        status: 'completed'
      }
    ]
  }
])

// 响应式数据
const activeStatus = ref('all')
const alertSound = ref(null)

// 计算属性
const filteredOrders = computed(() => {
  return orders.value.filter(order => {
    if (activeStatus.value === 'all') return true
    if (activeStatus.value === 'pending') return order.status === 0
    if (activeStatus.value === 'cooking') return order.status === 1
    return true
  }).sort((a, b) => b.priority - a.priority || new Date(a.createTime) - new Date(b.createTime))
})

const pendingCount = computed(() => orders.value.filter(o => o.status === 0).length)
const cookingCount = computed(() => orders.value.filter(o => o.status === 1).length)

// 方法
const formatTime = (timeStr) => {
  return timeStr ? timeStr.slice(11, 16) : '--'
}

const totalDishes = (order) => order.dishes.length
const totalQuantity = (order) => order.dishes.reduce((sum, d) => sum + d.quantity, 0)

const startCooking = (dish) => {
  dish.status = 'cooking'
  updateOrderStatus(dish)
  playAlertSound()
}

const completeDish = (dish) => {
  dish.status = 'completed'
  updateOrderStatus(dish)
  checkOrderComplete(dish)
}

const updateOrderStatus = () => {
  orders.value.forEach(order => {
    if (order.dishes.every(d => d.status === 'completed')) {
      order.status = 2
    } else if (order.dishes.some(d => d.status === 'cooking')) {
      order.status = 1
    } else {
      order.status = 0
    }
  })
}

const checkOrderComplete = (dish) => {
  const order = orders.value.find(o => o.dishes.includes(dish))
  if (order.dishes.every(d => d.status === 'completed')) {
    ElMessage.success(`${order.table} 桌所有菜品已完成！`)
  }
}

const playAlertSound = () => {
  if (alertSound.value) {
    alertSound.value.play().catch(() => { /* 处理自动播放限制 */ })
  }
}

const refreshOrders = () => {
  // 模拟数据刷新
  ElMessage.info('已刷新最新订单数据')
}

// 初始化更新状态
updateOrderStatus()
</script>

<style scoped>
.app-container {
  padding: 20px;
  height: 100vh;
  box-sizing: border-box;
  background: #f5f7fa;
}

.control-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.05);
}

.order-card {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 15px;
  padding: 15px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: all 0.3s;

  &.highlight-order {
    border-left: 4px solid #e6a23c;
    background: #fffdf5;
  }
}

.order-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.order-meta {
  display: flex;
  align-items: center;
  gap: 15px;

  .table-no {
    font-weight: 600;
    color: #333;
  }

  .order-time {
    color: #666;
    font-size: 0.9em;
  }
}

.dish-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  margin: 8px 0;
  background: #f8f9fa;
  border-radius: 6px;

  &.pending {
    border-left: 3px solid #909399;
  }

  &.cooking {
    border-left: 3px solid #e6a23c;
  }

  &.completed {
    border-left: 3px solid #67c23a;
  }
}

.dish-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

.dish-image {
  width: 80px;
  height: 60px;
  border-radius: 4px;
}

.dish-detail {
  h4 {
    margin: 0 0 5px 0;
    color: #333;
  }

  .meta {
    display: flex;
    gap: 15px;
    color: #666;
    font-size: 0.9em;
  }
}

.dish-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.total-info {
  color: #666;
  font-size: 0.9em;
}
</style>
